package org.sapmentors.wave;
 
import java.io.IOException;
import java.io.Writer;
import java.text.MessageFormat;

import javax.servlet.http.HttpServletResponse;

import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.pom.IEvent;
/**
 * Simple portal component which provides embedding of Waves in iView.
 * Based on the guide at http://code.google.com/intl/nb-NO/apis/wave/embed/guide.html
 * 
 * Portal component has parameters to define:
 * <ul><li>WaveId: default wavesandbox.com!w+iWcEB_o8%A (id of discusions related to SAP and wave integration)
 * <li>WaveServerUrl: default http://wave.google.com/a/wavesandbox.com/
 * <li>WaveEmbedJs: default http://wave-api.appspot.com/public/embed.js
 * </ul>
 * 
 * @author @dparnas
 */
public class WaveEmbeddedPortalComponent extends AbstractPortalComponent
{
   /**
    * We do not implement the doContent method as the doOnNodeReady replaces it.
    * This is because doContent adds all the SAP NetWeaver Portal framework HTML code,
    * something which interfers with the google wave embedding
    * 
    */
    public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
    {
    	
    }
    /**
     * doOnNodeReady is used to make sure no parts of the SAP NetWeaver Portal framework HTML code
     * is issued
     * 
     * The method writes HTML code which again through javascript triggers the loading
     * of the wave
     * 
     * 
     */
	protected void doOnNodeReady(IPortalComponentRequest request, IEvent event){
		HttpServletResponse response = request.getServletResponse(true);
		response.setContentType("text/html");
		
		Writer out;
		try {
			out = response.getWriter();
			
			IPortalComponentProfile profile = request.getComponentContext().getProfile();
			//Get the currently stored value in the profile
	
			String strWaveId;
			String strWaveServerUrl;
			String strWaveEmbedJsUrl;
	
			//get the parameter values from the iView
			strWaveId=profile.getProperty("WaveId");
			strWaveServerUrl=profile.getProperty("WaveServerUrl");
			strWaveEmbedJsUrl=profile.getProperty("WaveEmbedJs");
	
			//Java is not really ideal for handling long strings
			MessageFormat mfHTMLOutput= new MessageFormat(
			"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"+
			"	\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"+
			"<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:v=\"urn:schemas-microsoft-com:vml\">\n"+
			"  <head>\n"+
			"	<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n"+
			"	<title>Google Wave Embed API Example: Simple Wave</title>\n"+
			"	<script src=\"{2}\" type=\"text/javascript\"></script>\n"+
			"	<script type=\"text/javascript\">\n"+
			"	function initialize() '{'\n"+
			"	  var wavePanel = new WavePanel(''{1}'');\n"+
			"	  wavePanel.loadWave(''{0}'');\n"+
			"	  wavePanel.init(document.getElementById('waveframe'));\n"+
			"	'}'\n"+
			"	</script>\n"+
			"  </head>\n"+
			"  <body onload=\"initialize()\">\n"+
			"	<div id=\"waveframe\" style=\"width: 100%; height: 100%\"></div>\n"+
			"  </body>\n"+
			"</html>");
	
			Object arParameterValues[] = {strWaveId, strWaveServerUrl,strWaveEmbedJsUrl};
	
			String strHTML = mfHTMLOutput.format(arParameterValues);
			
			out.write(strHTML);
			out.close();
		} catch (IOException e) {
			//shouldn't happen so we don't really handle this exception
			e.printStackTrace();
		}


	}
}